<?php

namespace app\api\service;

use app\api\model\xyb\Order;
use app\api\model\xyb\Profit;
use app\api\model\xyb\Server;
use app\api\model\xyb\Wallets;
use app\common\model\xyb\AdminBelong;

class ProfitOrderService
{

    public static function calculateRiderIncome($user_id){
        $response = [
            'total_income'      => 0,
            'today_income'      => 0,
            'yesterday_income'  => 0,
            'seven_income'      => 0,
            'agent_income'      => 0,
            'order_num'         => 0,
            'bad_order_count'   => 0,
            'can_withdrawal_amount'=>0,
            'today_order_count' => 0,
        ];
        $profitModel = new Profit();
        $orderModel = new Order();
        $where = ['receive_id' => array('eq', $user_id)];
        $response['total_income'] = $profitModel->where($where)->sum('receive_profit');
        $response['today_income'] = $profitModel->where($where)->whereTime('createtime', 'today')->sum('receive_profit');
        $response['yesterday_income'] = $profitModel->where($where)->whereTime('createtime', 'yesterday')->sum('receive_profit');
        $response['seven_income'] = $profitModel->where($where)->whereTime('createtime', '>=', strtotime(date('-7 days')))->sum('receive_profit');
        $response['order_num'] = $orderModel->where($where)->where('status','in',[2,3,7,8])->count();
        $response['bad_order_count'] = 0;
        $response['today_order_count'] = $orderModel->where($where)->where('status','in',[2,3,7,8])->whereTime('createtime', 'today')->count();
        return $response;
    }

    public static function calculateAgentIncome($user_id){
        $response = [
            'total_income'      => 0,
            'today_income'      => 0,
            'yesterday_income'  => 0,
            'seven_income'      => 0,
            'order_num'         => 0,
            'bad_order_count'   => 0,
            'today_order_count' => 0,
        ];

        $profitModel = new Profit();
        $orderModel = new Order();
        $where = ['agent_id' => array('eq', $user_id)];
        $response['total_income'] = $profitModel->where($where)->sum('agent_profit');
        $response['today_income'] = $profitModel->where($where)->whereTime('createtime', 'today')->sum('agent_profit');
        $response['yesterday_income'] = $profitModel->where($where)->whereTime('createtime', 'yesterday')->sum('agent_profit');
        $response['seven_income'] = $profitModel->where($where)->whereTime('createtime', '>=', strtotime(date('-7 days')))->sum('agent_profit');
        $response['order_num'] = $orderModel->where($where)->where('status','in',[2,3,7,8])->count();
        $response['bad_order_count'] = 0;
        $response['today_order_count'] = $orderModel->where($where)->where('status','in',[2,3,7,8])->whereTime('createtime', 'today')->count();
        return $response;
    }
}